﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace bonus.Controllers
{
	/// <summary>
	/// 财务控制器
	/// </summary>
	public class FinanceController : Controller
	{
		int size = 10;//每页显示的数据

		/// <summary>
		/// 财务列表
		/// </summary>
		/// <returns></returns>
		public ActionResult Index()
		{
			return View();
		}

		/// <summary>
		/// 财务详情
		/// </summary>
		/// <returns></returns>
		public ActionResult FinancialDetails()
		{
			return View();
		}

		//财务分页数据
		[HttpPost]
		public JsonResult PartialFinance(int index = 1, int size = 10)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null)
			{
				return Json(new { Code = "FAIL", Msg = "用户未登陆" });
			}

			if (user.UserType == Models.User.TypeClass.ADMIN)
			{
				var filter = Builders<Models.Journal>.Filter.Where(p => p.Type == Models.Journal.TypeClass.JINZHANG);

				var count = tool.Count<Models.Journal>(filter);

				var page = Math.Ceiling(Convert.ToDouble(count) / size);//总页数

				var list = tool.GetMany<Models.Journal>(filter, "AddDT:-1", size, index);

				ICollection<object> Entities = new List<object>() { };
				foreach (var item in list)
				{

					object i = new
					{
						JournalNO = item.JournalNO,
						Remarks = item.Remarks,
						OrderMoney = item.OrderMoney.ToString("0.##"),
						OrderNo = string.IsNullOrEmpty(item.OrderNo) ? "0" : item.OrderNo,
						AddDT = item.AddDT.ToString("yyyy-MM-dd HH:mm:ss"),
						Name = item.Name,
						Rest = item.Rest.ToString("0.##"),
						Type = Models.Journal.TypeClass.GetType(item.Type),
						In = item.In.ToString("0.##"),
						Out = item.Out.ToString("0.##"),
						GetNO = item.GetNO,
						Title = item.Title,
						Operator = item.Operator,
					};
					Entities.Add(i);

				}
				return Json(new { Code = "OK", Data = Entities, total = count });//Totle:总条数  Count:总页数   Index 当前页
			}
			else
			{
				var filter = Builders<Models.Journal>.Filter.Where(p => p.GetNO == user.NO);
				var count = tool.Count<Models.Journal>(filter);

				var page = Math.Ceiling(Convert.ToDouble(count) / size);//总页数

				var list = tool.GetMany<Models.Journal>(filter, "AddDT:-1", size, index);

				ICollection<object> Entities = new List<object>() { };
				foreach (var item in list)
				{
					object i = new
					{
						JournalNO = item.JournalNO,
						Remarks = item.Remarks,
						OrderMoney = item.OrderMoney.ToString("0.##"),
						OrderNo = string.IsNullOrEmpty(item.OrderNo) ? "0" : item.OrderNo,
						Name = item.Name,
						AddDT = item.AddDT.ToString("yyyy-MM-dd HH:mm:ss"),
						Rest = item.Rest.ToString("0.##"),
						Type = Models.Journal.TypeClass.GetType(item.Type),
						In = item.In.ToString("0.##"),
						Out = item.Out.ToString("0.##"),
						GetNO = item.GetNO,
						Title = item.Title,
						Operator = item.Operator,
					};
					Entities.Add(i);

				}
				return Json(new { Code = "OK", Data = Entities, total = count });//Totle:总条数  Count:总页数   Index 当前页
			}


		}


		/// <summary>
		/// 个人财务流水
		/// </summary>
		/// <returns></returns>
		public ActionResult GetOneJournal(string no)
		{
			Repository.Repository tool = new Repository.Repository();

			var filter = Builders<Models.Journal>.Filter.Where(p => p.GetNO == no);

			var list = tool.GetMany(filter, "AddDT:-1");

			return View(list);
		}

		/// <summary>
		/// 获取单个订单财务信息
		/// </summary>
		/// <param name="no">单个订单</param>
		/// <returns></returns>
		public JsonResult GetOrderJournal(string no)
		{
			Repository.Repository tool = new Repository.Repository();

			var filter = Builders<Models.Journal>.Filter.Where(p => p.OrderNo == no && p.Type != Models.Journal.TypeClass.JINZHANG);
            var count = tool.Count<Models.Journal>(filter);
            var list = tool.GetMany(filter, "AddDT:-1");

			ICollection<object> Entities = new List<object>() { };
			foreach (var item in list)
			{
				object i = new
				{
					JournalNO = item.JournalNO,
					Remarks = item.Remarks,
					OrderMoney = item.OrderMoney.ToString("0.##"),
					OrderNo = string.IsNullOrEmpty(item.OrderNo) ? "0" : item.OrderNo,
					Name = item.Name,
					AddDT = item.AddDT.ToString("yyyy-MM-dd HH:mm:ss"),
					Rest = item.Rest.ToString("0.##"),
					Type = Models.Journal.TypeClass.GetType(item.Type),
					In = item.In.ToString("0.##"),
					Out = item.Out.ToString("0.##"),
					GetNO = item.GetNO,
					Title = item.Title,
					Operator = item.Operator,
				};
				Entities.Add(i);

			}
			return Json(new { Code = "OK", Data = Entities ,total = count });//Totle:总条数  Count:总页数   Index 当前页

			//return Json(new { Code = "OK", Msg = "查询成功", Data = list });
		}

	}
}